package com.ybkj.daijia.server.event.listener.zhuan;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderRecordEvent;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.zhuan.Order;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class ZhuanOrderRecordEventListener implements ApplicationListener<ZhuanOrderRecordEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ZhuanOrderRecordEventListener.class);


    @Autowired
    private ZhuancheService zhuancheService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    @Async
    public void onApplicationEvent(ZhuanOrderRecordEvent event) {

        Long orderId = (Long) event.getSource();
        Order order = zhuancheService.findOne(orderId);

        logger.debug("order record event:{}", order.getOrderNo());

        Setting setting = settingUtils.get();
        try {
            Date now = new Date();

            Long timestamp = System.currentTimeMillis();

            List<String> params = new LinkedList<String>();

            params.add(String.valueOf(order.getId()));
            params.add(order.getOrderNo());
            params.add(String.valueOf(now.getTime()));
            params.add(setting.getSecKey());
            params.add(String.valueOf(timestamp));

            Collections.sort(params);

            Sha1Hash sha1 = new Sha1Hash(params.toString());
            String signature = sha1.toString();

            StringBuilder sb = new StringBuilder();
            sb.append("orderId").append("=").append(order.getId());
            sb.append("&");
            sb.append("orderNum").append("=").append(order.getOrderNo());
            sb.append("&");
            sb.append("orderTime").append("=").append(now.getTime());
            sb.append("&");
            sb.append("secKey").append("=").append(setting.getSecKey());
            sb.append("&");
            sb.append("signature").append("=").append(signature);
            sb.append("&");
            sb.append("timestamp").append("=").append(String.valueOf(timestamp));

            String json = restTemplate.getForObject(
                "http://dj.easymin.com/api/rest/app/driverapp/addOrderRecord" + "?" + sb.toString(),
                String.class);

            ObjectMapper mapper = new ObjectMapper();

            mapper.readValue(json, Integer.class);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
